package com.ymatou.iapi.prod.testcase;

import java.util.Date;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.prod.parameter.GetProductInfoBean;
import com.ymatou.iapi.prod.service.GetProductInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.BuyerAppProdService;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.mongo.ActivityProductModelWapper;
import com.ymttest.database.mongo.LiveProductModelWapper;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.JsonBeanFile;

public class Ts_GetProductInfo {
	
	private static GetProductInfoBean getProductInfoBean;
	private static GetProductInfoCall getProductInfoCall;
	
	private static int sellerId = Integer.parseInt(EnvSetup.getData("zhangyi_seller002_id"));
	private static String sellerName = EnvSetup.getData("zhangyi_seller002_name");
	
	private static int activityStartId = Integer.parseInt(EnvSetup.getData("productActivityStartId"));
	private static String activityUser = EnvSetup.getData("AdminLoginUser");
	private static String activityPswd = EnvSetup.getData("AdminLoginPswd");
	
	private static String fileName = JsonBeanFile.CreateLiveForZyTpl;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商品详情");
	}

	@Before
	public void caseUp() throws Exception {
		getProductInfoBean = new GetProductInfoBean();
		getProductInfoCall = new GetProductInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getProductInfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_GetProductInfo_001() {
		 Logger.start(true,"商品详情接口数据与mongodb验证");
		 try {
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 

			 getProductInfoBean.setProductId(productId);
			 getProductInfoBean.setUserId(4085);
			 
			 getProductInfoCall.setData(getProductInfoBean);
			 getProductInfoCall.callService();
			 
			 this.verifyNormalData(productId, getProductInfoCall, 0);
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_GetProductInfo_002() {
		 Logger.start(true,"验证规格的可享受的vip价格是否与mongodb一致");
		 try {
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 
			 BuyerAppProdService prodService = new BuyerAppProdService();
			 
			 getProductInfoBean.setProductId(productId);
			 getProductInfoBean.setUserId(20226821);
			 
			 getProductInfoCall.setData(getProductInfoBean);
			 getProductInfoCall.callService();
			 
			 Logger.comment("getProductInfoCall send:" + getProductInfoCall.getEntityString());
			 Logger.comment("getProductInfoCall response:" + getProductInfoCall.getReturnData());
			 
			 //this.verifyNormalData(productId, getProductInfoCall);
			 
			 JSONArray skus = new JSONArray(getProductInfoCall.getResult().get("skus").toString());
			 String catalogId = new JSONObject(skus.get(0).toString()).getString("id");
			 
			 Logger.verifyEquals(prodService.getCatalogPriceFromMongo(2, productId, catalogId).getInt("Price"), 
					 new JSONObject(skus.get(0).toString()).getInt("price"), "验证规格的可享受的vip价格是否与mongodb一致");
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@SuppressWarnings("rawtypes")
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_GetProductInfo_003() {
		 Logger.start(true,"验证规格的可享受的新人价格是否与mongodb一致");
		 try {
			 Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			 
			 int userId = (Integer)user.get("userId");
			 
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 
			 BuyerAppProdService prodService = new BuyerAppProdService();
			 
			 getProductInfoBean.setProductId(productId);
			 getProductInfoBean.setUserId(userId);
			 
			 getProductInfoCall.setData(getProductInfoBean);
			 getProductInfoCall.callService();
			 
			 Logger.comment("getProductInfoCall send:" + getProductInfoCall.getEntityString());
			 Logger.comment("getProductInfoCall response:" + getProductInfoCall.getReturnData());
			 
			 //this.verifyNormalData(productId, getProductInfoCall);
			 
			 JSONArray skus = new JSONArray(getProductInfoCall.getResult().get("skus").toString());
			 String catalogId = new JSONObject(skus.get(0).toString()).getString("id");
			 
			 Logger.verifyEquals(prodService.getCatalogPriceFromMongo(1, productId, catalogId).getInt("Price"), 
					 new JSONObject(skus.get(0).toString()).getInt("price"), "验证规格的可享受的新人价格是否与mongodb一致");
	
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_GetProductInfo_004() {
		 Logger.start(true,"验证商品是否为直播商品");
		 try {
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 
			 int liveId = LiveCallService.createLiveInProgress(sellerName, sellerId, fileName).getJSONObject("Data").getInt("ActivityId");
			 SellerProductCallService.addProductInLive(sellerId, liveId, productId);
			 
			 getProductInfoBean.setProductId(productId);
			 getProductInfoBean.setUserId(4085);
			 
			 getProductInfoCall.setData(getProductInfoBean);
			
			 getProductInfoCall.callService();
			 
			 Logger.comment("getProductInfoCall send:" + getProductInfoCall.getEntityString());
			 Logger.comment("getProductInfoCall response:" + getProductInfoCall.getReturnData());
			 
			 this.verifyNormalData(productId, getProductInfoCall, 1);
			 Logger.verifyEquals(1, getProductInfoCall.getResult().getInt("productType"), "验证商品是否为直播商品");
			 
			 //LiveCallService.closeLive(liveId, sellerId);
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_GetProductInfo_005() {
		 Logger.start(true,"验证商品是否为活动商品");
		 try {
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 //String productId = "49ec3dc1-e037-4e20-bd13-5e01f5c293d6";
			 Thread.sleep(10000);
			 
			 SellerProductCallService.approveActivity(sellerId, activityUser, activityPswd, String.valueOf(activityStartId), productId);
			 
			 getProductInfoBean.setProductId(productId);
			 getProductInfoBean.setUserId(4085);
			 
			 getProductInfoCall.setData(getProductInfoBean);
			 
			 getProductInfoCall.callService();
			 
			 Logger.comment("getProductInfoCall send:" + getProductInfoCall.getEntityString());
			 Logger.comment("getProductInfoCall response:" + getProductInfoCall.getReturnData());
			 
			 this.verifyNormalData(productId, getProductInfoCall, 2);
			 Logger.verifyEquals(3, getProductInfoCall.getResult().getInt("productType"), "验证商品是否为活动商品");
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	public void verifyNormalData(String productId, GetProductInfoCall getProductInfoCall, int productType) throws Exception {
		 int tariffType = 0;
		 int length = 10086;
		 
		 ProductDetailModelWapper productDetailModelWapper = new ProductDetailModelWapper();
		 BuyerAppProdService prodService = new BuyerAppProdService();
		 Map<String,Object> productDetailModelMap = productDetailModelWapper.getProductId(productId);
		 JSONArray picsArray = new JSONArray(productDetailModelMap.get("PicList").toString());
		 JSONArray skus = new JSONArray(getProductInfoCall.getResult().get("skus").toString());
		 String catalogId = new JSONObject(skus.get(0).toString()).getString("id");
		 
		 JSONArray catalogList = new JSONArray(productDetailModelMap.get("CatalogList").toString());
		 JSONArray propertyList = new JSONObject(catalogList.get(0).toString()).getJSONArray("PropertyList");
		 JSONObject property = new JSONObject(propertyList.getString(0));
		 
		 Logger.verifyEquals(productDetailModelMap.get("ProductId"), 
				 getProductInfoCall.getResult().get("id"), "验证id是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap.get("Title"), 
				 getProductInfoCall.getResult().get("title"), "验证title是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap.get("Title"), 
				 getProductInfoCall.getResult().get("description"), "验证description是否与mongodb一致");
		 
		 for (int i = 0; i < picsArray.length(); i++) {
			 picsArray.get(i);
			 Logger.verifyEquals(picsArray.get(i), new JSONArray(getProductInfoCall.getResult().get("pics").toString()).get(i), 
					 "验证pics是否与mongodb一致");
		 }
		 
		 if (productType == 1) {
			 LiveProductModelWapper liveProductModelWapper = new LiveProductModelWapper();
			 
			 Logger.verifyEquals(this.getCST2TimeStamp(((Date) liveProductModelWapper.getByProductId(productId).get("StartTime")).toString()), 
					 this.changeUNIXTime(getProductInfoCall.getResult().getLong("putawayTime")), "验证putawayTime是否与mongodb一致");
			 Logger.verifyEquals(this.getCST2TimeStamp(((Date) liveProductModelWapper.getByProductId(productId).get("EndTime")).toString()), 
					 this.changeUNIXTime(getProductInfoCall.getResult().getLong("expireTime")), "验证expireTime是否与mongodb一致");
		 } else if (productType == 2) {
			 ActivityProductModelWapper activityProductModelWapper = new ActivityProductModelWapper();
			 
			 Logger.verifyEquals(this.getCST2TimeStamp(((Date) activityProductModelWapper.getLastActivityProductMapByProductId(productId).
					 get("BeginTimeOfProductInActivity")).toString()), this.changeUNIXTime(getProductInfoCall.getResult().getLong("putawayTime")), "验证putawayTime是否与mongodb一致");
			 Logger.verifyEquals(this.getCST2TimeStamp(((Date) activityProductModelWapper.getLastActivityProductMapByProductId(productId).
					 get("EndTimeOfProductInActivity")).toString()), this.changeUNIXTime(getProductInfoCall.getResult().getLong("expireTime")), "验证expireTime是否与mongodb一致");
		 }
		 else {
			 Logger.verifyEquals(this.getCST2TimeStamp(((Date) productDetailModelMap.get("ValidStart")).toString()), 
					 this.changeUNIXTime(getProductInfoCall.getResult().getLong("putawayTime")), "验证putawayTime是否与mongodb一致");
			 Logger.verifyEquals(this.getCST2TimeStamp(((Date) productDetailModelMap.get("ValidEnd")).toString()), 
					 this.changeUNIXTime(getProductInfoCall.getResult().getLong("expireTime")), "验证expireTime是否与mongodb一致");
		 }
		 
		 if (productDetailModelMap.get("TariffType").toString().equalsIgnoreCase("0") 
				 && productDetailModelMap.get("Freight").toString().equalsIgnoreCase("0.00"))
			 tariffType = 3;
		 
		 Logger.verifyEquals(tariffType, getProductInfoCall.getResult().get("tariffType"), 
				 "验证tariffType是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap.get("DeliveryMethod"), 
				 getProductInfoCall.getResult().get("deliveryType"), "验证deliveryType是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap.get("SellerId"), 
				 getProductInfoCall.getResult().get("sellerId"), "验证sellerId是否与mongodb一致");
		 Logger.verifyEquals(new JSONObject(catalogList.get(0).toString()).getString("CatalogId"), 
				 new JSONObject(skus.get(0).toString()).getString("id"), "验证CatalogId是否与mongodb一致");
		 Logger.verifyEquals(catalogList.length(), skus.length(), "验证Catalog size是否与mongodb一致");
		 
		 if (property.get("Name").toString().equalsIgnoreCase("null") && property.get("Value").toString().equalsIgnoreCase("null")
				 && property.get("PicUrl").toString().equalsIgnoreCase("null"))
			 length = 0;
		 
		 Logger.verifyEquals(length, 
				 new JSONObject(skus.get(0).toString()).getJSONArray("properties").length(), "验证无规格属性");
		 
		 int stock = new JSONObject(skus.get(0).toString()).getInt("stock");
		 
		 if (stock <= 5)
			 Logger.verifyEquals("仅剩5件", 
					 new JSONObject(skus.get(0).toString()).getString("stockDesc"), "验证库存小于5时的库存描述");
		 else
			 Logger.verifyEquals("库存充足", 
					 new JSONObject(skus.get(0).toString()).getString("stockDesc"), "验证库存大于5时的库存描述");
		 
		 if (productType == 2) {
			 Logger.verifyEquals(prodService.getCatalogStockFromMongo(1, productId, catalogId).getInt("Stock"), 
					 stock, "验证规格的库存数量是否已mongodb一致");
		 } else {
			 Logger.verifyEquals(prodService.getCatalogStockFromMongo(0, productId, catalogId).getInt("Stock"), 
					 stock, "验证规格的库存数量是否已mongodb一致");
		 }
		 Logger.verifyEquals(prodService.getCatalogPriceFromMongo(0, productId, catalogId).getInt("Price"), 
				 new JSONObject(skus.get(0).toString()).getInt("original"), "验证规格的原价是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap.get("NoReasonReturn"), 
				 getProductInfoCall.getResult().getBoolean("isNoReasonReturn"), "验证isNoReasonReturn是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap.get("BrandId"), 
				 getProductInfoCall.getResult().getInt("brandId"), "验证brandId是否与mongodb一致");	
		 Logger.verifyEquals(productDetailModelMap.get("Introduction"), 
				 getProductInfoCall.getResult().getString("intro"), "验证intro是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap.get("IsPspProduct"), 
				 getProductInfoCall.getResult().getBoolean("isPspProduct"), "验证isPspProduct是否与mongodb一致");
		 
		 //目前没有订金模式支付，所有的都为全款支付
		 Logger.verifyEquals(0, getProductInfoCall.getResult().getInt("payMode"), "验证payMode是否与mongodb一致");
	}
	
	//将unix时间戳毫秒数置为000
	public long changeUNIXTime(long timeStamp) {
		return Long.parseLong(String.valueOf(timeStamp).substring(0, 10).concat("000"));
	}
	
	public long getCST2TimeStamp(String dateStr) {
		return YMTDateUtil.CST2TimeStamp(dateStr);
	}
}
